//
//  Device_GetBatteryInfoHandler.swift
//  mall-ios
//
//  Created by Meak Tsui on 2018/11/28.
//  Copyright © 2018 Meak Tsui. All rights reserved.
//

import Foundation
import UIKit
import MTHybrid

extension Api {
    static let GetBatteryInfo = Api("getBatteryInfo")
}

/// 获取电池状态
class Device_GetBatteryInfoHandler: MTEventHandler {
    var ns: Ns {
        return .Device
    }
    
    var action: Api {
        return .GetBatteryInfo
    }
    
    func handler(_ msg: MTHybridMsg) {
        UIDevice.current.isBatteryMonitoringEnabled = true
        let level = Int(UIDevice.current.batteryLevel * 100)
        let batteryState = UIDevice.current.batteryState == .charging
        UIDevice.current.isBatteryMonitoringEnabled = false
        msg.callbackClosure?(["level":level,"inCharging":batteryState])
    }
    
    
}
